package com.qf.courtesycard.grabtickets.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.courtesyard.domain.entity.Ticket;
import com.qf.courtesyard.domain.entity.User;
import com.qf.courtesyard.domain.vo.ResultVo;
import com.qf.courtesycard.grabtickets.entity.Userforticket;
import com.qf.courtesycard.grabtickets.fegin.TicketClient;
import com.qf.courtesycard.grabtickets.fegin.UserTokenClient;
import com.qf.courtesycard.grabtickets.mapper.UserforticketMapper;
import com.qf.courtesycard.grabtickets.rabbitmp.TicketLogMq;
import com.qf.courtesycard.grabtickets.service.IUserforticketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lx
 * @since 2023-03-10
 */
@Service
public class UserforticketServiceImpl extends ServiceImpl<UserforticketMapper, Userforticket> implements IUserforticketService {

    @Autowired
    private UserTokenClient userTokenClient;

    @Autowired
    private TicketClient ticketClient;

    @Autowired
    private TicketLogMq ticketLogMq;

    @Override
    public String rushTicket(String name, String phoneNum) throws Exception {
        //调用user模块获取token
        ResultVo userResultVo = userTokenClient.getToken(name, phoneNum);
        String token = (String) userResultVo.get("data");
        if (token == null) {
            throw new Exception("token为空");
        }
        //调用provide模块获取优惠券
        ResultVo resultVo = ticketClient.distributeTicket();
        Map ticketMap = (Map) resultVo.get("data");
        Ticket ticket = assembleTicket(ticketMap);
//        ObjectMapper objectMapper = new ObjectMapper();
//        Ticket ticket = objectMapper.readValue(ticketToJson, Ticket.class);
        if (ticket == null) {
            throw new Exception("抢券失败");
        }
        //生成抢券流水日志
        //根据用户电话，调用user模块获取用户信息
        ResultVo userInfo = userTokenClient.getUser(phoneNum);
        Map userMap = (Map) userInfo.get("data");
        User user = assembleUser(userMap);
        if (user == null) {
            throw new Exception("当前用户不存在");
        }
        String ticketLog = createTicketLogs(ticket, user);
        //抢券日志发mq
        ticketLogMq.send("ticket_exchange", null, ticketLog);

        return "抢券成功";
    }

    private User assembleUser(Map userMap) {
        User user = new User();
        user.setId((Integer) userMap.get("id"));
        user.setName((String) userMap.get("name"));
        user.setPhone((String) userMap.get("phone"));
        return user;
    }

    private Ticket assembleTicket(Map ticketMap) throws ParseException {
        Ticket ticket = new Ticket();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ticket.setId((String) ticketMap.get("id"));
        ticket.setPrice((Double) ticketMap.get("price"));
        ticket.setScope((String) ticketMap.get("scope"));
        ticket.setCreatetime(sdf.parse((String) ticketMap.get("createtime")));
        ticket.setExpirationdate(sdf.parse((String) ticketMap.get("expirationdate")));
        ticket.setStatus((Integer) ticketMap.get("status"));
        return ticket;
    }

    private String createTicketLogs(Ticket ticket, User user) throws JsonProcessingException {
        Userforticket userforticket = new Userforticket();
        ObjectMapper objectMapper = new ObjectMapper();
        userforticket.setName(user.getName());
        userforticket.setPhone(user.getPhone());
        userforticket.setUserId(user.getId());
        userforticket.setTicketId(ticket.getId());
        userforticket.setTicketdetailed(ticket.getScope() + ticket.getPrice() + ticket.getCreatetime() + ticket.getExpirationdate() + ticket.getStatus());
        String tickeLog = objectMapper.writeValueAsString(userforticket);
        return tickeLog;
    }
}
